/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.uwb.support.base;

import android.os.Parcel;

import androidx.annotation.Nullable;

import java.util.Arrays;

/** Provides parameter versioning. */
public class ProtocolVersion {
    public static ProtocolVersion fromString(String protocol) {
        String[] parts = protocol.split("\\.", -1);
        if (parts.length != 2) {
            throw new IllegalArgumentException("Invalid protocol version: " + protocol);
        }

        return new ProtocolVersion(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
    }

    private final int mMajor;
    private final int mMinor;

    public ProtocolVersion(int major, int minor) {
        mMajor = major;
        mMinor = minor;
    }

    protected ProtocolVersion(Parcel in) {
        mMajor = in.readInt();
        mMinor = in.readInt();
    }

    @Override
    public String toString() {
        return mMajor + "." + mMinor;
    }

    public int getMajor() {
        return mMajor;
    }

    public int getMinor() {
        return mMinor;
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(new int[] {mMajor, mMinor});
    }

    @Override
    public boolean equals(@Nullable Object other) {
        if (other instanceof ProtocolVersion) {
            ProtocolVersion otherProtocol = (ProtocolVersion) other;
            return otherProtocol.mMajor == mMajor && otherProtocol.mMinor == mMinor;
        }
        return false;
    }
}
